// 生成指定起止数之间的连续整数数组
export function getRandArray(start, end, length=false, prefix="0"){
	if(start > end) return [];
	
	let array = [];
	for(let num=start; num<=end; num++){
		// 判断是否限制长度
		if(length){
			let n = Number.parseFloat(num).toFixed(0);
			if(n.length < length) array.push(prefix+num);
			if(n.length > length) array.push(n.substr(0, length));
			if(n.length == length) array.push(num);
		}else{
			array.push(num);
		}
	}
	
	return array;
}

// 获取日期时间
export function date(isFull = true){
	let oDate = new Date();
	let year = oDate.getFullYear();
	let month = oDate.getMonth()+1;
	let day = oDate.getDate();
	let hours = oDate.getHours();
	let minutes = oDate.getMinutes();
	let seconds = oDate.getSeconds();
	
	if(isFull){
		if(month < 10) month = '0'+month;
		if(day < 10) day = '0'+day;
		if(hours < 10) hours = '0'+hours;
		if(minutes < 10) minutes = '0'+minutes;
		if(seconds < 10) seconds = '0'+seconds;
	}
	
	return { year, month, day, hours, minutes, seconds };
}

// 获取今天和上月的今天
export function getLastMonth(){
    var now=new Date();
    var year = now.getFullYear();//getYear()+1900=getFullYear()
    var month = now.getMonth() +1;//0-11表示1-12月
    var day = now.getDate();
    var dateObj = {};
    if(parseInt(month)<10){
        month = "0"+month;
    }
    if(parseInt(day)<10){
        day = "0"+day;
    }

    dateObj.now = year + '-'+ month + '-' + day;

    if (parseInt(month) ==1) {//如果是1月份，则取上一年的12月份
        dateObj.last = (parseInt(year) - 1) + '-12-' + day;
        return dateObj;
    }

    var  preSize= new Date(year, parseInt(month)-1, 0).getDate();//上月总天数
    if (preSize < parseInt(day)) {//上月总天数<本月日期，比如3月的30日，在2月中没有30
        dateObj.last = year + '-' + month + '-01';
        return dateObj;
    }
	
	// var dayStr = parseInt(day)-1;
	// dayStr = dayStr < 10 ? '0'+dayStr : dayStr;
    if(parseInt(month) <=10){
        dateObj.last = year + '-0' + (parseInt(month)-1) + '-' + day;
        return dateObj;
    }else{
        dateObj.last = year + '-' + (parseInt(month)-1) + '-' + day;
        return dateObj;
    }
}

// 将秒数转为时分秒格式
export function secondsToFormatTime(totalSeconds){
	// 计算天数
	let days = totalSeconds > 86400 ? Number.parseInt(totalSeconds / 86400) : '0';
	// 除去天数后的剩余秒数
	let restHourSeconds = totalSeconds - days*24*3600;
	
	// 计算小时数
	let hours = restHourSeconds > 3600 ? Number.parseInt(restHourSeconds / 3600) : '0';
	
	// 除去小时后的剩余秒数
	let restSeconds = restHourSeconds - hours*3600;
	let minutes = restSeconds > 60 ? Number.parseInt(restSeconds / 60) : '0';
	
	// 除去小时和分钟后的描述
	let lastSeconds = restSeconds - minutes * 60;
	
	return {
		days,
		hours: hours < 10 ? '0'+hours : hours,
		minutes: minutes < 10 ? '0'+minutes : minutes,
		seconds: lastSeconds < 10 ? '0'+lastSeconds : lastSeconds
	};
}

// 校验是否为空
export function isNull(str){
  let res = /\S/.test(str)
  return !res
}

// 随机数
export function createRandomCode(length = 6, number = false) {
	if (length == 0) return;
	const data = number ? ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] : ["0", "1", "2", "3", "4", "5", "6", "7",
		"8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
		"V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r",
		"s", "t", "u", "v", "w", "x", "y", "z"
	];
	let code = '';
	for (let i = 0; i < length; i++) {
		let index = Number.parseInt(Math.random() * (number ? 10 : 61));
		code += data[index];
	}
	return code;
}

// JSON字符串(不规则)转JSON对象
export function jsonStrToObject(objStr, trimLength=1){
	// 去掉首的字符
	let str = objStr.substr(trimLength);
	str = str.substr(0, str.length - trimLength);
	
	// 按照指定字符拆分成数组
	let obj = {};
	let strArr = str.split(',');
	strArr.forEach((item,index)=>{
		let itemArr = item.split('=');
		let [key, value] = itemArr;
		
		// 去掉首尾字符串
		key = key.replace(/^(\s|")+|(\s|")+$/g, '');
		value = value.replace(/^(\s|")+|(\s|")+$/g, '');
		obj[key] = value;
	});
	return obj;
}